package com.seocoo.worker.order.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.net.Uri;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.seocoo.worker.R;
import com.seocoo.worker.order.widget.rsp.RspOrderDetailAddress;

/***
 *订单详情地址布局
 */
public class OrderDetailAddressView extends ConstraintLayout {

    private TextView tv_address_name;
    private TextView tv_address_detail;
    private TextView tv_address_detail1;
    private TextView tv_user_name;
    private TextView tv_user_phone;
    private boolean showCallPhone;
    private boolean showCargoCode;
    private ImageView iv_call_phone1;
    private ImageView iv_call_phone2;
    private TextView tv_cargo_code_name;
    private TextView tv_cargo_code;
    private String phone = "";
    private String merchantContact = "";

    public OrderDetailAddressView(Context context) {
        super(context);
        initAttrs(null);
        initView(context);
    }

    public OrderDetailAddressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initView(context);
    }

    public OrderDetailAddressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView(context);
    }

    @SuppressLint("CustomViewStyleable")
    private void initAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.orderAddressView);
            showCallPhone = attributes.getBoolean(R.styleable.orderAddressView_show_call_phone, false);
            showCargoCode = attributes.getBoolean(R.styleable.orderAddressView_show_cargo_code, false);
            attributes.recycle();
        }
    }

    private void initView(Context context) {
        View view = inflate(context, R.layout.order_detail_address_layout, this);
        tv_address_name = view.findViewById(R.id.tv_address_name);
        tv_address_detail = view.findViewById(R.id.tv_address_detail);
        tv_address_detail1 = view.findViewById(R.id.tv_address_detail1);
        tv_user_name = view.findViewById(R.id.tv_user_name);
        tv_user_phone = view.findViewById(R.id.tv_user_phone);
        iv_call_phone1 = view.findViewById(R.id.iv_call_phone1);
        iv_call_phone2 = view.findViewById(R.id.iv_call_phone2);
        tv_cargo_code = view.findViewById(R.id.tv_cargo_code);
        tv_cargo_code_name = view.findViewById(R.id.tv_cargo_code_name);
        if (showCallPhone) {
            iv_call_phone1.setVisibility(VISIBLE);
            iv_call_phone2.setVisibility(VISIBLE);
        } else {
            iv_call_phone1.setVisibility(GONE);
            iv_call_phone2.setVisibility(GONE);
        }
        if (showCargoCode) {
            tv_cargo_code.setVisibility(VISIBLE);
            tv_cargo_code_name.setVisibility(VISIBLE);
        } else {
            tv_cargo_code.setVisibility(GONE);
            tv_cargo_code_name.setVisibility(GONE);
        }
        iv_call_phone1.setOnClickListener(v -> {
            if (TextUtils.isEmpty(merchantContact)) {
                return;
            }
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("tel:" + merchantContact));
            if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                context.startActivity(intent);
            } else {
                Toast.makeText(context, "无法拨打电话，请检查应用权限设置", Toast.LENGTH_SHORT).show();
            }
        });
        iv_call_phone2.setOnClickListener(v -> {
            if (TextUtils.isEmpty(phone)) {
                return;
            }
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("tel:" + phone));
            if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                context.startActivity(intent);
            } else {
                Toast.makeText(context, "无法拨打电话，请检查应用权限设置", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void initData(RspOrderDetailAddress data) {
        if (data == null) {
            return;
        }
        if (tv_address_name != null) {
            tv_address_name.setText(data.getCargoAddress());
        }
        if (tv_address_detail != null) {
            tv_address_detail.setText(data.getCargoAddressDetail());
        }
        if (tv_address_detail1 != null) {
            tv_address_detail1.setText(data.getUserAddress());
        }
        if (tv_user_name != null) {
            tv_user_name.setText(data.getUserName());
        }
        if (tv_user_phone != null) {
            String userPhone = data.getUserPhone();
            tv_user_phone.setText(userPhone);
            phone = userPhone;
            merchantContact = data.getMerchantContact();
        }

        if (tv_cargo_code != null) {
            if (TextUtils.isEmpty(data.getPickupCode())) {
                tv_cargo_code.setText("");
                tv_cargo_code.setVisibility(GONE);
                tv_cargo_code_name.setVisibility(GONE);
            } else {
                tv_cargo_code.setText(data.getPickupCode());
                tv_cargo_code.setVisibility(VISIBLE);
                tv_cargo_code_name.setVisibility(VISIBLE);
            }
        }

    }

    public TextView getTv_user_phone() {
        return tv_user_phone;
    }

    public void setVisibilityPhone(int visibilityPhone) {
        iv_call_phone1.setVisibility(visibilityPhone);
        iv_call_phone2.setVisibility(visibilityPhone);
    }

    public void setVisibilityPickUpCode(boolean show) {
        if (show) {
            tv_cargo_code.setVisibility(VISIBLE);
            tv_cargo_code_name.setVisibility(VISIBLE);
        } else {
            tv_cargo_code.setText("");
            tv_cargo_code.setVisibility(GONE);
            tv_cargo_code_name.setVisibility(GONE);
        }

    }

}
